<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$setting=uni_setting('cash');
$setting['desc'] = str_replace("\n","</br>",$setting['desc']);
$merchsetting=$this->plugin_setting('setting');
$setting['percent']=round($merchsetting['feerate'],2)*1;
if($_RQ['op']=='get'){
	$credit_types=credit_types();
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit5','credit6','mobile','avatar','nickname'));
	$return['item']=$member;
	$return['item']['banklist']=array();
	$type_array=array();
	$check=strstr($setting['withdrawtype'],'0');
	if($check){
		$type_array[0]='提现到微信钱包';
	}else{
        $type_array[0]='无';
    }
	$check=strstr($setting['withdrawtype'],'1');
	if($check){
		$type_array[1]='提现到支付宝';
        $post1=pdo_get('core_member_cash',array('type'=>1,'mid'=>$_SESSION['uid']),array('alipay','realname'),'id desc');
	}
	$check=strstr($setting['withdrawtype'],'2');
	if($check){
		$type_array[2]='提现到银行卡';
        $post2=pdo_get('core_member_cash',array('type'=>2,'mid'=>$_SESSION['uid']),array('bankname','bankcard','realname'),'id desc');
        $post2['bankcard1']=$post2['bankcard'];
	}
    if($post1 || $post2) {
        $return['post'] = array_merge($post1, $post2);
    }
	foreach($merchsetting['cash_set'] as $k=>&$cv){
		if($cv['rate'] && $cv['type']){
			$cv['title']=$credit_types[$cv['type']];
			$cv['fee']=$member['credit'.$cv['type']];
			$merchtixian=1;
			if(!$return['post']['fee_type']){
				$return['post']['fee_type']=$return['post']['fee_type']?:$cv['type'];
				$return['post']['rate']=$cv['rate'];
				$return['post']['creditfee']=$cv['fee'];
			}
		}else{
			unset($merchsetting['cash_set'][$k]);
		}
	}
	if(!$merchtixian){
		exi('暂时不支持商户虚拟币提现！','','goback');
	}
	$return['cash_set']=$merchsetting['cash_set'];
	$setting['type_array']=$type_array;
	$return['setting']=$setting;
    $return['post']['type']='no';
    if($_SESSION['wxapp_id'] && !$_SESSION['fromh5wxapp']){
        $return['post']['type']='1';
    }
	exi($return);
}
if($_RQ['op']=='post'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$post=get_postdata();
	if($post['type']=='no'){
		exi('请选择提现方式！','erroe');
	}
	if(!$post['password'] || $post['password']!=$member['paypassword']){
		exi('支付密码不正确！','erroe');
	}
	if(!$post['fee']){
		exi('请输入提现额度','error');
	}
	if(!$post['fee_type']){
		exi('请选择提现币类型','error');
	}
	$fee=$post['fee'];
	if(is_numeric($fee)){
	}else{
		exi('提现额度必须是数字！','error');
	}
	$money=$fee/$post['rate'];
	$service_money=round($money*$setting['percent']/100,2);
	if($post['type']==1){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['alipay']){
			exi('请输入支付宝帐号','error');
		}
		if(!$post['alipay1']){
			exi('请确认帐号','error');
		}
		if($post['alipay1']!=$post['alipay']){
			exi('确认帐号与支付宝帐号不匹配！','error');
		}
	}
	if($post['type']==2){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['bankname']){
			exi('请输入所属银行','error');
		}
		if(!$post['bankcard']){
			exi('请银行卡号','error');
		}
		if(!$post['bankcard1']){
			exi('请确认卡号','error');
		}
		if($post['bankcard']!=$post['bankcard1']){
			exi('确认卡号与银行卡号不匹配！','error');
		}
	}
	model('core/member/cash')->check_cash_xunibi($_SESSION['uid'],$fee,$money,$post['fee_type']);
	$cash=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'ordersn'=>get_ordersn('core_member_cash','ordersn','CASH'),
		'money'=>$money,
		'type'=>intval($post['type']),
		'realname'=>$post['realname'],
		'alipay'=>$post['alipay'],
		'bankname'=>$post['bankname'],
		'bankcard'=>$post['bankcard'],
		'service_money'=>$service_money,
		'remark'=>'虚拟币提现',
		'credit_type'=>1,
		'credit_fee'=>$fee,
		'createtime'=>TIMESTAMP,
		'status'=>0,
	);
	$cash['cash_money']=$cash['money']-$cash['service_money'];
	pdo_insert('core_member_cash',$cash);
	$id=pdo_insertid();
	$type_array=array('提现到微信钱包','提现到支付宝','提现到银行卡');
	$typetitle=$type_array[$cash['type']];
	model('core/member/cash')->notice_tixian($_SESSION['uid'],$money,$typetitle);
	if(model('core/member/cash')->setting['check'] || $params['to_type'] !=0){
		exi('申请提现成功，请等待管理员审核！','','goback');
	}else{
		model('core/member/cash')->pay($id);
		exi('提现成功！','success','goback');
	}
}